/// <reference types="vitest" />
/// <reference types="vite/client" />
import { ConfigEnv, UserConfig } from 'vite';
import solidPlugin from 'vite-plugin-solid';
import legacy from '@vitejs/plugin-legacy';
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons';
import { visualizer } from 'rollup-plugin-visualizer';
import { resolve } from 'path';
import pkg from './package.json';
// https://vitejs.dev/config/
const shouldAnalyze = process.env.ANALYZE || false;
export default ({ command, mode }: ConfigEnv): UserConfig => {
  console.log('mode', mode);
  return {
    base: mode === 'dev' ? '' : '/solid-ui/',
    envDir: './env',
    plugins: [
      solidPlugin(),
      legacy({
        targets: ['Android>=4.1', 'iOS>=9', 'Chrome>=30'],
        additionalLegacyPolyfills: ['regenerator-runtime/runtime.js']
      }),
      createSvgIconsPlugin({
        iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
        symbolId: 'icon-[name]',
        inject: 'body-first',
        customDomId: '__svg__icons__dom__'
      })
    ],
    server: {
      port: 8100
    },
    resolve: {
      alias: [{ find: '@', replacement: resolve(__dirname, './src') }]
    },
    build: {
      // target: 'esnext',
      chunkSizeWarningLimit: 1200,
      assetsDir: 'static/img/',
      rollupOptions: {
        plugins: shouldAnalyze ? [visualizer({ filename: './dist/_stats.html' })] : [],
        output: {
          chunkFileNames: 'static/js/[name]-[hash].js',
          entryFileNames: 'static/js/[name]-[hash].js',
          assetFileNames: 'static/[ext]/[name]-[hash].[ext]'
        }
      }
      // lib: {
      //   // Could also be a dictionary or array of multiple entry points
      //   entry: resolve(__dirname, 'src/components/index.ts'),
      //   formats: ['es', 'cjs'],
      //   fileName: (format) => (format === 'es' ? 'soid-ui.mjs' : 'soid-ui.cjs'),
      //   name: 'soid-ui'
      //   // the proper extensions will be added
      // },
      // rollupOptions: {
      //   // 确保外部化处理那些你不想打包进库的依赖
      //   external: [
      //     ...Object.keys(pkg.peerDependencies),
      //     'solid-js',
      //     'solid-js/web',
      //     'solid-js/store'
      //   ],
      //   output: {
      //     // 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
      //     globals: {
      //       'solid-js': 'solidJs'
      //     }
      //   }
      // }
    },
    test: {
      environment: 'jsdom',
      transformMode: {
        web: [/.[jt]sx?/]
      },
      include: ['src/components/**/{__test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
      setupFiles: ['node_modules/@testing-library/jest-dom/extend-expect.js'],
      // otherwise, solid would be loaded twice:
      deps: { registerNodeLoader: true },
      threads: false,
      isolate: false
    }
  };
};
